/**
 * 
 */
package com.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.util.watchdog.ConfigFileChangedAdapter;
import com.util.watchdog.ConfigFileChangedListener;
import com.utils.string.StringUtils;


public class MenuUtil implements ConfigFileChangedListener
{	
	public static synchronized MenuUtil getInstance()
	{
		if (instance == null)
			instance = new MenuUtil();
		return instance;
	}
	
	private static MenuUtil instance = null;
	
	public List<Menu> getMenu()
	{
		return this.menuList;
	}
	
	public int getMenuCount()
	{
		return menuCount;
	}
	
	public String getMenuFormString()
	{
		String str ="";
		
		return str;
	}
		
	protected MenuUtil()
	{
		File file = new File(MenuUtil.class.getResource("/").getFile() + "Menu.properties");
		this.load(file);
		ConfigFileChangedAdapter adapter = ConfigFileChangedAdapter.getInstance();
		adapter.addListener(file, this);
	}
	
	public void load(File file)
	{
		menuList = new java.util.ArrayList<Menu>();
		menuCount = 0;
		try
		{
			Properties props = new Properties();
			props.load(new java.io.FileInputStream(file));
			String menu = props.getProperty("menu", "");
			if (menu.length() > 0)
			{
				String[] menuKeys = menu.split(",");
				for (int n = 0; n < menuKeys.length; n++)
				{
					menuList.add(new Menu(props, menuKeys[n]));
					menuCount++;
				}
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	private List<Menu> menuList;
	private static int menuCount;

	@Override
	public void fileChanged(File file) {
		if (file != null)
			this.load(file);
	}
	
	public static class Menu
	{
		private int index;
		private String key;
		private String title;
		private String action;
		private List<Menu> submenu;
		
		public Menu(Properties props, String key) {
			this.key  = key;
			this.index = 0;
			this.title = "";
			this.action = "";
			this.submenu = new ArrayList<MenuUtil.Menu>();
			try
			{
				this.index = StringUtils.toInt(props.getProperty("menu." + key + ".index", "0"));
				this.title = new String(props.getProperty("menu." + key + ".title", "").getBytes("ISO-8859-1"), "UTF-8");
				this.action = new String(props.getProperty("menu." + key + ".action", "").getBytes("ISO-8859-1"), "UTF-8");
				String menu = new String(props.getProperty("menu." + key + ".submenu", "").getBytes("ISO-8859-1"), "UTF-8");
				if (menu.length() > 0)
				{
					String[] menuKeys = menu.split(",");
					for (int n = 0; n < menuKeys.length; n++)
					{
						submenu.add(new Menu(props, key + "." + menuKeys[n]));
						menuCount++;
					}
				}
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
			}
		}
		
		public int getIndex() {
			return index;
		}
		public String getTitle() {
			return title;
		}
		public String getAction() {
			return action;
		}
		public List<Menu> getSubmenu() {
			return submenu;
		}

		/**
		 * @return the key
		 */
		public String getKey() {
			return key;
		}
	}
	
	public static void main(String[] argv)
	{
		MenuUtil mm = new MenuUtil();
		
		System.out.println(mm.menuList.size());
	}

}
